#include <QCoreApplication>
#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/param.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>
#include <string.h>

#define D_SOURCE_PATH "/agvwork"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    char szCurrentPath[1024]=D_SOURCE_PATH;
        //char* returnPath = getcwd(szCurrentPath,1024);

    char szExecCmd[1024]={0};
    //start get desk path

//	sprintf(szExecCmd,"%s","desk=~/桌面/;if [ -d $desk ];then echo $desk>/tmp/deskpath.data;fi");
//	system(szExecCmd);
//	sprintf(szExecCmd,"%s","desk=~/Desktop/;if [ -d $desk ];then echo $desk>/tmp/deskpath.data;fi");
//	system(szExecCmd);




//	FILE *fpDeskPath;
//	char szCurrentPath[2048] = {0};

//	fpDeskPath=fopen("/tmp/deskpath.data","r");
//	fread(szCurrentPath, 1, 1024, fpDeskPath);

//	fclose(fpDeskPath);

//	int deskPathLength = strlen(szCurrentPath);
//	if(deskPathLength<=0)
//	{
//		return -1;
//	}
//	szCurrentPath[deskPathLength-1]=0;
    //end get desk path

    //unzip

    sprintf(szExecCmd,"unzip -qo %s/agvHmiPackage.zip -d %s",szCurrentPath,szCurrentPath);
    system(szExecCmd);
    sprintf(szExecCmd,"rm %s/agvHmiPackage.zip",szCurrentPath);
    system(szExecCmd);
    sprintf(szExecCmd,"chmod -R 777 %s/agvwork",szCurrentPath);
    system(szExecCmd);
    system("echo '0'>/agvwork/forkflag.data");

    FILE *fp;
    char tmp[10] = {0};

    while(1)
    {
        fp=fopen("/agvwork/forkflag.data","r");
        if(fread(tmp, 1, 1, fp) == 1)
        {
            fclose(fp);
            fp = NULL;
            if(tmp[0] == '0')
            {
            sprintf(szExecCmd,"cp -rf %s/agvwork/* /agvwork/",szCurrentPath);
            system(szExecCmd);
            //删除解压缩后的文件夹
                sprintf(szExecCmd,"rm -rf %s/agvwork",szCurrentPath);
            system(szExecCmd);

                system("cp -rf /agvwork/AgvHmi/baseBag/hmiProject/lib/x86/* /agvwork/AgvHmi/baseBag/tool/lib-x86");

                system("chmod 777 /agvwork/AgvHmi/baseBag/hmiProject/bin/x86/hmi.sh");
                system("sh /agvwork/AgvHmi/baseBag/hmiProject/bin/x86/hmi.sh");
                system("echo '1'>/agvwork/forkflag.data");
            }
            else if(tmp[0] == '1')
            {
                sleep(1);
                //正常开启ing
                break;//暂时break
            }
            else if(tmp[0] == '2')//调用校准
            {
                system("killall hmiMain");
                system("echo 1-killall hmiMain $?");

                system("rm /etc/pointercal");
                system("echo rm /etc/pointercal $?");

                system("sleep 0.1");
                system("echo sleep 0.1 $?");

                system("/opt/tslib/bin/ts_calibrate");
                system("echo /opt/tslib/bin/ts_calibrate $?");

                system("killall ts_calibrate");
                system("echo killall ts_calibrate $?");

                system("echo '0'>/agvwork/forkflag.data");//重启
            }
            else if(tmp[0] == '3')//重启系统
            {
                system("killall  hmiMain");
                system("sleep 1");
                system("echo sleep 1 $?");
                system("echo '0'>/agvwork/forkflag.data");//重启

                system("reboot");
            }

        }
        else
        {
            fclose(fp);
                fp = NULL;

            sprintf(szExecCmd,"for var in %s/agvwork/AgvHmi/baseBag/hmiProject/lib/x86/*so.1.0.0;do mv $var ${var%%so.1.0.0}so.1;done",szCurrentPath);
            system(szExecCmd);
            sprintf(szExecCmd,"cp -rf %s/agvwork/AgvHmi/baseBag/hmiProject/bin/* /agvwork/AgvHmi/baseBag/hmiProject/bin/",szCurrentPath);
            system(szExecCmd);
            system("sh /agvwork/AgvHmi/baseBag/hmiProject/bin/x86/hmi.sh");
            system("echo '1'>/agvwork/forkflag.data");
        }
    }

   while(1)
    {
        sleep(11);
    }


    return a.exec();
}
